<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Session;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class House extends Backend
{

    /**
     * House模型对象
     * @var \app\admin\model\House
     */
    protected $model = null;
    protected $hid = null;
    protected $adminId = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\House;
        $this->hid = Session::get("admin")["hotelid"];
        $this->adminId = Session::get("admin")["id"];
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
            return $this->view->fetch();
    }

    public function getRooms($hotelId){

        $hotelId = $this->request->param("hotelId");
        if($hotelId){
            $id = $hotelId;
        }else{
            $model = model('Hotels');
            //获取第一个酒店id 根据权限
            if($this->adminId == 1){
                $id = $model->where("h_status='normal'")->value("hid");
            }else{
                $id = $model->where("h_status='normal'")->where('hid','in',$this->hid)->value("hid");
            }

        }

        //酒店房间数据
        $list = $this->model
            ->where("h_id=$id")
            ->field("h_storey")
            ->group("h_storey")
            ->select();



        $list = collection($list)->toArray();

        $resdata=[];
        foreach ($list as $k=>$v){
            $resdata[$k]["storeyHtml"] = $v["h_storey"]."F-房间分布图";
            //获取楼层房间
            $lists = $this->model
                ->where("h_id=$id and h_storey=".$v['h_storey'])
                ->field("id,h_statusText,h_title")
                ->order("id asc")
                ->select();
            $lists = collection($lists)->toArray();
            $resdata[$k]["storeydata"] = $lists;
        }

        if($resdata){
            $result = array("code"=>1,"rows" => $resdata);
        }else{
            $result = array("code"=>0,"rows" => []);
        }
        return json($result);

    }


     /*
     * 添加酒店
     */

     public function add(){

         //获取酒店列表
         $model = model('Hotels');
         $hotelList = $model->where("h_status='normal'")->field("hid,h_title")->select();
         //获取第一个酒店id
         $hotelnum = $model->where("h_status='normal'")->value("h_storey");
         $hotelNumArr = $this->reNum($hotelnum);
         //获取房间设施
         $res = model('Config')->where("name='houseservice'")->field("value")->find();
         if($res){
             $restmp = $res->toarray()["value"];
             $serviceList = json_decode($restmp,true);
         }

         $this->assign("serviceList",$serviceList);

         $this->assign("hotelNumArr",$hotelNumArr);
         $this->assign("hotelList",$hotelList);

         return $this->view->fetch();

     }
     //动态返回酒店楼层数
    public function AjaxHotelNum(){
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $id = $params["hotelid"];
            //获取第一个酒店id
            $model = model('Hotels');
            $hotelnum = $model->where("hid={$id}")->value("h_storey");
            if($hotelnum < 1 ){
                $this->error("error");
            }else{
                $hotelNumArr = $this->reNum($hotelnum);
                $this->success("success","",$hotelNumArr);
            }


        }
    }

     //返回楼层号数组
    public function reNum($id){
        $index = array();
        if($id == 1){
            array_push($index,1);
        }else{
            for ($i = 1;$i<=$id;$i++){
                array_push($index,$i);
            }
        }

        return $index;
    }




}
